package com.ecoolex.hotel.saas.api.graphql;

import java.math.BigDecimal;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMember;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMemberManager;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;
import com.ecoolex.hotel.saas.entity.Department;
import com.ecoolex.hotel.saas.entity.SalesUser;
import com.ecoolex.hotel.saas.entity.SalesUserWallet;
import com.ecoolex.hotel.saas.entity.SalesUserWalletDetail;
import com.ecoolex.hotel.saas.entity.SalesUserWalletWithdrawalApply;
import com.ecoolex.hotel.saas.entity.Store;
import com.ecoolex.hotel.saas.pojo.AddSalesUserDTO;
import com.ecoolex.hotel.saas.ws.PresaleCouponOrderService;
import com.ecoolex.hotel.saas.ws.SalesUserService;
import com.ecoolex.hotel.saas.ws.StoreService;
import com.ecoolex.user.system.pojo.LoginUserRoot;


@GraphQL2Module(type = Type.User, description = "商户用户")
public class SalesUserGraphql {

	@Reference
	private SalesUserService salesUserService;
	
	@Reference
	private StoreService storeService;
	
	@Autowired
	private ConsumerMemberManager memberManger;
	
	@Reference
	private PresaleCouponOrderService presaleCouponOrderService;
	
	@TypeField(type = LoginUserRoot.class,field = "salesUser", description = "商户下的销售员身份")
	public SalesUser salesUser(@Source LoginUserRoot user, @NonNull Integer storeId) {
		return salesUserService.getByUserIdAndStoreId(user.getUserId(), storeId);
	}

	@TypeField(type = LoginUserRoot.class,field = "explicitPhone", description = "显式手机号")
	public String explicitPhone(@Source LoginUserRoot user){
		ConsumerMember consumerMember = memberManger.getByUserId(user.getUserId());
		return consumerMember == null ? null : consumerMember.getPhone();
	}
	
	@TypeField(type = SalesUser.class,field = "storeInfo", description = "所属商户信息")
	public Store store(@Source SalesUser source) {
		return storeService.getStore(source.getStoreId());
	}
	
	@TypeField(type = SalesUser.class,field = "department", description = "所属部门信息")
	public Department department(@Source SalesUser source) {
		return storeService.getDepartment(source.getDepartmentId(), source.getStoreId());
	}
	
	@QueryField(field = "salesUserWallet", description = "用户钱包")
	public SalesUserWallet get(@NonNull Integer storeId, ConsumerMemberAuthority user) {
		user.assertLogin();
		return salesUserService.getSalesUserWalletByUserIdAndStoreId(storeId, user.userId());
	}
	
	@QueryField(field = "salesUserWalletDetailList", description = "用户钱包明细列表")
	public Pagination<SalesUserWalletDetail> query(@NonNull Integer walletId, @NonNull Page page, ConsumerMemberAuthority user){
		user.assertLogin();
		return salesUserService.queryByWalletId(walletId, page);
	}
	
	@QueryField(field = "salesUserWalletWithdrawalApply", description = "用户钱包提现记录详情")
	public SalesUserWalletWithdrawalApply getSalesUserWalletWithdrawalApply(@NonNull Integer applyId, ConsumerMemberAuthority user) {
		user.assertLogin();
		return salesUserService.getSalesUserWalletWithdrawalApply(applyId);
	}
	
	@QueryField(field = "salesUserWalletWithdrawalApplyList", description = "用户钱包提现记录列表")
	public Pagination<SalesUserWalletWithdrawalApply> querySalesUserWalletWithdrawalApply(@NonNull Integer storeId,@NonNull Page page, ConsumerMemberAuthority user){
		user.assertLogin();
		return salesUserService.querySalesUserWalletWithdrawalApply(storeId, user.userId(), page);
	}
	
	@MutationField(field = "submitSalesUserWalletWithdrawalApply", description = "提交钱包提现申请")
	public SalesUserWalletWithdrawalApply submitApply(@NonNull Integer storeId, @NonNull Integer walletId, @NonNull BigDecimal applyAmt, ConsumerMemberAuthority user) {
		user.assertLogin();
		SalesUserWallet salesUserWallet = salesUserService.getSalesUserWalletByUserIdAndStoreId(storeId, user.userId());
		Assert.isTrue(Check.eq(walletId, salesUserWallet.getWalletId()),ResultCode.VERIFY.build(1), "参数异常");
		return salesUserService.submitApply(walletId, applyAmt, user.userName());
	}
	
	@MutationField(field = "authSalesUser", description = "销售员身份认证")
	public SalesUser authSalesUser(@NonNull AddSalesUserDTO add, ConsumerMemberAuthority user) {
		user.assertLogin();
		ConsumerMember consumerMember = memberManger.getByUserId(user.userId());
		add.setUserId(user.userId());
		add.setPhone(consumerMember.getPhone());
		return salesUserService.addSalesUser(add, user.userName());
	}
	
	@MutationField(field = "presaleCouponCodeVerify", description = "预售券码核销")
	public Boolean couponCodeVerify(@NonNull Integer storeId, @NonNull String couponCode, ConsumerMemberAuthority user) {
		user.assertLogin();
		return presaleCouponOrderService.couponCodeVerify(storeId, couponCode, user.userName());
	}
}
